package com.summarecon.qualitycontrol;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.summarecon.database.DatabaseBlueprint;
import com.summarecon.singleton.ColorPicker;
import com.summarecon.utility.DrawView;

import de.ipcas.colorPicker.ColorPickerDialog;

public class BlueprintLocationActivity extends Activity {
	public byte[] accImage;
	private DrawView myDrawView;
	public Dialog colorDialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_blueprint_location);

		DatabaseBlueprint dbBlueprint = new DatabaseBlueprint(BlueprintLocationActivity.this);
		dbBlueprint.open();
		accImage = dbBlueprint.getImageData();
		dbBlueprint.close();
		
		RelativeLayout parent = (RelativeLayout) findViewById(R.id.drawBluePrintLayer);
		myDrawView = new DrawView(this);
		parent.addView(myDrawView);
		myDrawView.setColor(0xFF000000);
		
		Button btnColor = (Button)findViewById(R.id.colorBtn2);  
		btnColor.setOnClickListener(new OnClickListener(){
			boolean opened = false;
			@Override
			public void onClick(View arg0) {
				if(!opened){
					opened = true;
					colorDialog = new ColorPickerDialog(BlueprintLocationActivity.this);
					colorDialog.show();
					colorDialog.setOnDismissListener(new OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							Log.i("COLOR", ""+ColorPicker.getColor());
							myDrawView.setColor(ColorPicker.getColor());
							opened = false;
						}
					});
				}
			}
		});
		Button btnClear = (Button)findViewById(R.id.clearBtn2);  
		btnClear.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				myDrawView.clear();
			}
		});
		Button btnSave = (Button)findViewById(R.id.saveBtn2);  
		btnSave.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				Intent myIntent = new Intent(getBaseContext(), DefectLevelActivity.class);
				startActivity(myIntent);
				finish();
			}
		});
		if(accImage != null){
			ImageView image=(ImageView)findViewById(R.id.imageBluePrint);
			image.setImageBitmap(BitmapFactory.decodeByteArray( accImage,0,accImage.length));
		}

	}


}
